﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Data.SqlClient;
using System.IO;
using LHS.Core.TableInfo;
using System.Collections.Specialized;

namespace LHS.Win
{
    public partial class frmLapHinhAnh : Form
    {
        LHS.Core.Clinical.ClinicalController objController = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmLapHinhAnh(frmHoSoBenhAn fHoSoBenhAn)
        {
            this._fHoSoBenhAn = fHoSoBenhAn;
            this.InitializeComponent();
        }

        private void btBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPEG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap Files (*.bmp)|*.bmp";
            dialog.FilterIndex = 1;
            dialog.RestoreDirectory = true;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.path = dialog.FileName.ToString();
                this.ext = Path.GetExtension(this.path);
                this.lbPath.Text = this.path;
                this.picPatients.Image = Image.FromFile(this.path);
                this.picPatients.SizeMode = PictureBoxSizeMode.Normal;
                this.picPatients.ClientSize = new Size(700, 550);
            }
        }

        private void btPrint_Click(object sender, EventArgs e)
        {
            if (this.picPatients.Image != null)
            {
                PrintDocument document = new PrintDocument();
                document.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                document.Print();
            }
            else
            {
                MessageBox.Show("Vui lòng chọn ảnh", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            string str = this._fHoSoBenhAn.clrISN.ToString();
            if (this.tbMoTa.Text == "")
            {
                MessageBox.Show("Vui lòng nhập mô tả", "Lỗi nhập", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (File.Exists(this.path))
            {
                object objPic = objController.GetIDENT_CURRENT(InfoClinicalPicture.TABLENAME);
                if (objPic == null || Convert.ToInt64(objPic.ToString()) == 0)
                    return;
                string destFileName = Application.StartupPath + @"\PatientImages\" + Convert.ToString(Convert.ToInt64(objPic.ToString()) + 1) + this.ext;

                File.Copy(this.path, destFileName, true);
                try
                {
                    ListDictionary param = new ListDictionary();
                    param.Add(InfoClinicalPicture.CLPISN, str);
                    param.Add(InfoClinicalPicture.CLPPICTUREDESCRIPTION, this.tbMoTa.Text);
                    param.Add(InfoClinicalPicture.CLPPICTURE, destFileName);

                    objController.Exec_StoreProc("sp_ClinicalPicture_Insert", param);

                    MessageBox.Show("Ảnh đã được lưu", "Chèn Hình", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.tbMoTa.Text = "";
                }
                catch (Exception exception)
                {
                    Class.ShowException(exception);
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn ảnh!", "Chưa chọn ảnh", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btThoat_Click(object sender, EventArgs e)
        {
            base.Close();
        }
        private void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(this.picPatients.Image, e.Graphics.VisibleClipBounds);
            e.HasMorePages = false;
        }

    }
}